<?php

namespace app\command\controller;

use think\Controller;
use think\Db;

class Clear extends Controller
{
    private $tableList = [
        "transaction_data_hk",
        "transaction_data_sft",
        "money_log",
    ];

    private $days = 365;

    public function index(){
        $time = time() - $this->days * 24 * 60 * 60;

        foreach ($this->tableList as $key => $val){
            Db::startTrans();
            try {
                Db::name($val)->where(" trading_time < {$time} ")->delete();

                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                file_put_contents("clear_log" . date("Ym") . ".txt", "errorInfo:" . $e->getMessage() . PHP_EOL, FILE_APPEND | LOCK_EX);
            }
        }
    }
}
